let session = {
  get (key) {
    return JSON.parse(sessionStorage.getItem(key))
  },
  set (key, value) {
    sessionStorage.setItem(key, JSON.stringify(value))
  },
  remove (key) {
    sessionStorage.removeItem(key)
  },
  clear () {
    sessionStorage.clear()
  }
}

let local = {
  get (key) {
    return JSON.parse(localStorage.getItem(key))
  },
  set (key, value) {
    localStorage.setItem(key, JSON.stringify(value))
  },
  remove (key) {
    localStorage.removeItem(key)
  },
  clear () {
    localStorage.clear()
  }
}

function getUserSettings (keyBaseOn) {
  return JSON.parse(localStorage.getItem(keyBaseOn)) || {}
}

let localBaseOnKey = {
  get (key, keyBaseOn) {
    return getUserSettings(keyBaseOn)[key]
  },
  set (key, value, keyBaseOn) {
    let curSettings = getUserSettings(keyBaseOn)
    curSettings[key] = value
    localStorage.setItem(keyBaseOn, curSettings)
  },
  remove (key, keyBaseOn) {
    let curSettings = getUserSettings(keyBaseOn)
    delete curSettings[key]
    localStorage.setItem(keyBaseOn, curSettings)
  },
  clear (keyBaseOn) {
    localStorage.removeItem(keyBaseOn)
  },
  clearAll () {
    localStorage.clear()
  }
}

// 基于SessionStorage
export {session as sessionStorage}
// 基于LocalStorage
export {local as localStorage}
// 基于用户Id的LocalStorage
export {localBaseOnKey as localStorageBaseOnKey}
